package org.jeecg.modules.yw.mapper;

import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jeecg.modules.yw.entity.Appointment;
import org.jeecg.modules.yw.vo.EnterPriseVo;

@Mapper
public interface AppointmentMapper extends BaseMapper<Appointment> {
    @Insert("INSERT INTO `appointment` (id, appointId, appointTime, createTime, appointType, orderId, name, bzzId) "
    		+ "VALUES(#{id}, #{appointId}, #{appointTime}, #{createTime},#{appointType},#{orderId}, #{name}, #{bzzId})")
    public int insertAppointment(Appointment appointment);    
    
    
    @Update("update `appointment` set appointType = #{appointType} where appointId = #{appointId} and orderId = #{orderId}")
    public int updataeAppointment(Appointment appointment);    
    
    /**
     * 已预约接收人员
     * @param appointment
     * @return
     */
    @Select("select * from `appointment` where appointId = #{appointId} and DATE_FORMAT(appointTime, '%Y-%m-%d') = #{appointTime} and appointType = 1 and delFlag = 0")
    public List<Appointment> selectAppointAccpt(Appointment appointment);
    
    
    /**
     * 未预约工人(工单池)
     * @param appointment
     * @return
     */
    @Select("select id, name, picture from `enterprise` where roleId = 2 and isLeader = 2 and delFlag = 0 and city_code in (#{cityCode}) and id not in (select appointId from appointment where delFlag = 0 and DATE_FORMAT(appointTime, '%Y-%m-%d')= #{yyTime}) and id not in (select userId from attendance where DATE_FORMAT(attendDate, '%Y-%m-%d') = #{yyTime})")
    public List<EnterPriseVo> selectAppointAvailable(@Param("yyTime") String yyTime,@Param("bzzId") String bzzId, @Param("cityCode")  String cityCode);
    
    
    
    /**
     * 订单已预约工人列表(预约当天)
     * @param appointment
     * @return
     */
    @Select("select appointId id, (select name from `enterprise` where id=appointId) name, " +
            "(select picture from `enterprise` where id=appointId) picture," +
            "(select name from `enterprise` where id=bzzId) bzzName, appointType, orderId, appointTime, bzzId " +
            "from `appointment` " +
            "where DATE_FORMAT(appointTime, '%Y-%m-%d') = #{appointTime} " +
            "and appointType = '1' " +
            "and orderId != #{orderId} " +
            "and bzzId = #{bzzId} " +
            "and delFlag = 0 " +
            "order by appointType, appointId")
    public List<EnterPriseVo> selectAppointsByOrderIdAndAppointTime(@Param("bzzId")  String bzzId, @Param("appointTime")  String appointTime, @Param("orderId")  String orderId);
    
    
    
    /**
     * 订单已预约工人列表
     * @param orderId
     * @return
     */
    @Select("select appointId id, (select name from `enterprise` where id=appointId) name, (select picture from " +
            "`enterprise` where id=appointId) picture,(select name from `enterprise` where id=bzzId) bzzName,appointTime, " +
            "appointType, orderId from `appointment` where orderId = #{orderId} and appointType in (0,1,2) " +
            " order by createTime,appointType, appointId ")
    public List<Appointment> selectAppointsByOrderId(@Param("orderId")  String orderId);
    
    
    /**
     * 员工请假
     * @param appointment
     * @return
     */
    @Update("update `appointment` set appointType = 3 where appointId = #{appointId} and DATE_FORMAT(appointTime, '%Y-%m-%d') = #{appointTime} and delFlag = 0")
    public int updateHoliday(Appointment appointment);
    
    
    /**
     * 凌晨12点清除工单池
     * @param appointment
     * @return
     */
    @Update("update `appointment` set delFlag = 1 where DATE_FORMAT(appointTime, '%Y-%m-%d') = #{appointTime}")
    public int clearWorkers(@Param("appointTime")  String appointTime);
    
    @Select("SELECT * FROM `appointment` WHERE `appointId` = #{id} AND `appointTime` >= #{startTime} AND `appointTime` < #{endTime}")
    List<Appointment> selectAppointmentsByCondition(@Param("id") String id, @Param("startTime") String startTime, @Param("endTime") String endTime);
    
    
    
//    /**
//     * 强制工人拒绝
//     * @param appointment
//     * @return
//     */
//    @Update("update `appointment` set appointType = 2 where appointId = #{appointId} and orderId = #{orderId} and DATE_FORMAT(appointTime, '%Y-%m-%d') = #{appointTime}")
//    public int updateAppointmentDeny(@Param("appointId")  String appointId,@Param("orderId")  String orderId, @Param("appointTime")  String appointTime);

    
    /**
     * 强制工人拒绝
     * @param appointment
     * @return
     */
    @Update("update `appointment` set appointType = 2 where appointId = #{appointId} and orderId = #{orderId}")
    public int updateAppointmentDeny(@Param("appointId")  String appointId,@Param("orderId")  String orderId, @Param("appointTime")  String appointTime);

}
